package com.zlx.guguanjia.mapper.provider;

import org.apache.ibatis.annotations.Param;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @Author 周罗校
 * @Description
 * @ClassName SysRoleSqlProvider
 * @date 2020.11.04 13:20:56
 * 该版权归周罗校所有，最终解释权以官方为准
 */
public class SysRoleSqlProvider {
    public String selectPage(Map<String,Object> map){
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT sro.*,sof.`name` office_name FROM sys_role sro,sys_office sof " +
                "WHERE " +
                "sro.del_flag = '0' ");
        if (map.containsKey("dScope")&&!StringUtils.isEmpty(map.get("dScope"))){
            sb.append("AND sro.data_scope= #{dScope} ");
        }
        if (map.containsKey("oid")&&!StringUtils.isEmpty(map.get("oid"))){
            sb.append("AND sro.office_id = #{oid} ");
        }
        if (map.containsKey("name")&&!StringUtils.isEmpty(map.get("name"))){
            sb.append("AND sro.`name` LIKE CONCAT('%',#{name},'%') ");
        }
        if (map.containsKey("remarks")&&!StringUtils.isEmpty(map.get("remarks"))){
            sb.append("AND sro.remarks  LIKE CONCAT('%',#{remarks},'%') ");
        }
        sb.append("AND sro.office_id = sof.id");
        return sb.toString();
    }

    public String deleteBatch(@Param("rid") long rid, @Param("ids") long[] ids){
        StringBuilder sb = new StringBuilder();
        sb.append("DELETE  from sys_user_role where role_id=#{rid} and user_id in ");
        sb.append("(");
        for (int i = 0; i < ids.length; i++) {
            sb.append("#{ids["+i+"]},");
        }
        sb.deleteCharAt(sb.length()-1);
        sb.append(")");
        return sb.toString();
    }

    public String  insertBatch(@Param("rid") long rid, @Param("cids") List<Long> cids){
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO `sys_user_role`(`id`, `role_id`, `user_id`, " +
                "`create_by`, `create_date`, `update_by`, `update_date`, `del_flag`) VALUES ");
        for (int i = 0; i < cids.size(); i++) {
            sb.append("(NULL, #{rid}, #{cids["+i+"]}, NULL, NOW(), NULL, NOW(), '0'),");
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }

    public String  insertBatchResources(@Param("rid")Long rid,@Param("sids") List<Integer> sids){
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO `guguanjia`.`sys_role_resource` ( `id`, `role_id`, `resource_id`, `create_by`, `create_date`, `update_by`, `update_date`, `del_flag` ) VALUES ");
        for (int i = 0; i < sids.size(); i++) {
            sb.append("( NULL, #{rid}, #{sids["+i+"]}, NULL, NOW(), NULL, NOW(), '0' ),");
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }
    public String insertRoleOffices(@Param("rid") long rid,@Param("offices") List<Integer> offices){
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO `guguanjia`.`sys_role_office`(`role_id`, `office_id`, `create_by`, `create_date`, `update_by`, `update_date`, `del_flag`) VALUES ");

        for (int i = 0; i < offices.size(); i++) {
            sb.append("(#{rid},#{offices["+i+"]},null,now(),null,now(),0),");
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }
}
